package com.example.demo.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.common.Result;
import com.example.demo.entity.WorkExperience;
import com.example.demo.service.WorkExperienceService;
import com.example.demo.utils.DateUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.regex.Pattern;


/**
 * 工作经历表(WorkExperience)表控制层
 *
 * @author makejava
 * @since 2023-12-19 08:33:38
 */
@RestController
@RequestMapping("workExperience")
public class WorkExperienceController {
    /**
     * 服务对象
     */
    @Autowired
    private WorkExperienceService workExperienceService;


    /**
     * 新增/编辑数据
     *
     * @param workExperience 实体对象
     * @return 新增/编辑结果
     */
    @PostMapping("doAdd")
    public Result doAdd(@RequestBody WorkExperience workExperience) {
        String timestampPattern = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z$";
        if (workExperience.getStartTime()!=null){
            if (Pattern.matches(timestampPattern, workExperience.getStartTime())) {
                String startConvert= DateUtil.convertDate(workExperience.getStartTime());
                workExperience.setStartTime(startConvert);
            }
        }
        if (workExperience.getEndTime()!=null){
            if (Pattern.matches(timestampPattern, workExperience.getEndTime())) {
                String endConvert= DateUtil.convertDate(workExperience.getEndTime());
                workExperience.setEndTime(endConvert);
            }
        }
        try {
            if (workExperience.getId()==null){
                workExperienceService.save(workExperience);
                return Result.success("添加成功");
            }else {
                UpdateWrapper<WorkExperience> wrapper=new UpdateWrapper<>();
                wrapper.eq("id",workExperience.getId());
                workExperienceService.update(workExperience,wrapper);
                return Result.success("编辑成功");
            }

        }catch (Exception e){
            return Result.error("参数错误");
        }
    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("delete")
    public Result delete(Integer id) {
        try {
            QueryWrapper<WorkExperience> wrapper=new QueryWrapper<>();
            wrapper.eq("id",id);
            workExperienceService.remove(wrapper);
            return Result.success("删除成功");
        }catch (Exception e){
            return Result.error("参数错误");
        }
    }
}

